var inited = false;
$(document).ready(function () {
    console.log("loading abnormal");
    initOptions();
})

function initOptions() {
    //配置流量异常气泡图
    var option = getStreamAbnormalBubbleOption();
    var myChart = echarts.init(document.getElementById('streamAbnormalChart'));
    myChart.setOption(option);
    //配置访客访问异常树图
    //option = getVisitorAbnormalTreeOption();
    option=getVisitorAbnormalMapOption();
    myChart = echarts.init(document.getElementById('visitorAbnormalChart'));
    myChart.setOption(option);
    //配置端口访问异常柱状图
    option = getPortAbnormalBarOption();
    myChart = echarts.init(document.getElementById('portAbnormalChart'));
    myChart.setOption(option);
    //配置异常事件饼图
    option = getAbnormalEventsPieOption();
    myChart = echarts.init(document.getElementById('abnormalEventsChart'));
    myChart.setOption(option);

    inited = true;
}

function loadWebStreamData() {

}

function loadFirewallStreamData() {

}

function loadAbnormalData() {

    if (!inited) {
        setTimeout(loadAbnormalData, 100);
        return;
    }
    //获取数据
    var data = isrealMode ? REAL_MODE_DATA : QUERY_MODE_DATA;
    var abnormalData = data.abnormalData.data;
    //处理数据
    var ipAbnormalData = {},
        portAbnormalData = {},
        streamAbnormalData = [],
        visitorAbnormalData = {},
        num = [0, 0, 0, 0],
        result = [],
        ipTableData = [],
        dsc = "";

    abnormalData.forEach((event, index) => {
        switch (event.eventType) {
            case "ipAbnormal":
                ipAbnormalData[event.feature['dstIp']] ? ipAbnormalData[event.feature['dstIp']] += 1 : ipAbnormalData[event.feature['dstIp']] = 1;
                dsc = "IP访问异常";
                num[0]++;
                break;
            case "portAbnormal":
                portAbnormalData[event.feature['dstPort']] ? portAbnormalData[event.feature['dstPort']] += 1 : portAbnormalData[event.feature['dstPort']] = 1;
                dsc = "端口访问异常";
                num[1]++;
                break;
            case "streamAbnormal":
                streamAbnormalData.push([event.startTime, Math.floor((Date.parse(event.endTime) - Date.parse(event.startTime)) / 1000), event.feature['value']]);
                dsc = "访问流量异常";
                num[2]++;
                break;
            case "visitorAbnormal":
/*                 let city = event.feature['srcCity'];
                let ip = event.feature['srcIp'];
                if (visitorAbnormalData[city]) {
                    if (visitorAbnormalData[city][ip]) {
                        visitorAbnormalData[city][ip] += 1;
                    } else
                        visitorAbnormalData[city][ip] = 1;
                } else {
                    visitorAbnormalData[city] = {};
                    visitorAbnormalData[city][ip] = 1;
                } */
                visitorAbnormalData[event.feature['srcCity']] ? visitorAbnormalData[event.feature['srcCity']] += 1 : visitorAbnormalData[event.feature['srcCity']] = 1;
                dsc = "访客访问异常";
                num[3]++;
                break;
            default:
                break;
        };
        result.push([index + 1, event.startTime, dsc]);
    });
    //更新异常事件比例图
    var chart = echarts.getInstanceByDom(document.getElementById('abnormalEventsChart'));
    var option={
        series: [{
            data: [{
                    name: "IP访问异常",
                    value: num[0]
                },
                {
                    name: "端口访问异常",
                    value: num[1]
                },
                {
                    name: "访问流量异常",
                    value: num[2]
                },
                {
                    name: "访客访问异常",
                    value: num[3]
                }
            ]
        }]
    };
    chart.setOption(option);
    //更新异常事件表格
    $("#abnormalEventsTable").DataTable({
        data:result,
        columns: [
            { title: "序号" },
            { title: "发生时间" },
            { title: "异常类型" }  
        ],
        paging:true,
        pageLength:7,
        destroy:true
    });

    //更新流量异常气泡图
    var chart = echarts.getInstanceByDom(document.getElementById('streamAbnormalChart'));
    var option={
        series: {
            data: streamAbnormalData
        }
    };
    chart.setOption(option);
    //更新IP访问异常表格
    result = [];
    var index = 1;
    for (const dstIp in ipAbnormalData) {
        result.push([index++, dstIp, ipAbnormalData[dstIp] + "次"]);
    };

    $("#ipAbnormalTable").DataTable({
        data:result,
        columns: [
            { title: "序号" },
            { title: "目的IP" },
            { title: "次数" }  
        ],
        scrollY:"180px",
        scrollCollapse:true,
        destroy:true
    });
    //更新访客访问异常树图
/*     var totalNum = 0;
    result = [];
    var childResult = [];
    for (const city in visitorAbnormalData) {
        let num = 0;
        childResult = [];
        for (const ip in visitorAbnormalData[city]) {
            let value = visitorAbnormalData[city][ip];
            totalNum += value;
            num += value;
            childResult.push({
                name: `${ip}\n(共发起${value}次)`,
                value: value
            });
        };
        result.push({
            name: `${city}\n共发起${num}次`,
            value: num,
            children: childResult
        });
    };
    var chart = echarts.getInstanceByDom(document.getElementById('visitorAbnormalChart'));
    var option={
        series: [{
            data: [{
                name: "受监测目标",
                value: totalNum,
                children: result
            }]
        }]
    };
    chart.setOption(option); */

    //更新访客异常地图
    chartResultData=[];
    var coord;
    for (const city in visitorAbnormalData) {
        coord=parseCityToCoord(city);
        if(city!="不详" && city!="域外")
            chartResultData.push([coord[0],coord[1],visitorAbnormalData[city],city]);
    }
    var chart = echarts.getInstanceByDom(document.getElementById('visitorAbnormalChart'));
    option={
        series:{
            data:chartResultData
        }
    }
    chart.setOption(option);


    //更新端口访问异常柱状图
    let color = ['#003366', '#006699', '#4cabce', '#dd545d'];
    index = 0;
    let xAxisData = [];
    result = [];
    for (const port in portAbnormalData) {
        xAxisData.push(port);
        result.push({
            value: portAbnormalData[port],
            itemStyle: {
                color: color[index++]
            }
        })
    }
    var chart = echarts.getInstanceByDom(document.getElementById('portAbnormalChart'));
    var option={
        xAxis: [{
            data: xAxisData
        }],
        series: [{
            data: result
        }]
    };
    chart.setOption(option);

}

function loadAttackData() {

}